﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PlanningGIS.ManageUtility.Modules;
using PlanningGIS.Util.Security;

namespace PlanningGIS.ManageUtility.DataAccess
{
    /// <summary>
    /// 用户数据操作对象
    /// </summary>
    public class UserAccess
    {
        /// <summary>
        /// 获取用户标识信息，及ID、Name
        /// </summary>
        /// <param name="isOnlyUser">是否仅显示普通用户</param>
        /// <returns></returns>
        public static DataTable GetUserTag(bool isOnlyUser = false)
        {
            string sql = "select t.id,t.name,t.department_id from USERINFO t where t.admin_type ='FFF'  order by t.department_id,t.name ";
            if (!isOnlyUser)
                sql = "select t.id,t.name,t.department_id from USERINFO t order by t.department_id,t.name ";
            return ManageHelper.DBContext.GetDataTable(sql);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="user">用户名称</param>
        /// <param name="password">密码</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static UserInfo Login(string user, string password, string key = "")
        {
            if (ManageHelper.Administrator.Equals(user))
            {
                if (!Encoding.UTF8.GetString(ManageHelper.AdminPassword).Equals(password)) return null;
                return new UserInfo()
                {
                    Login_Name = ManageHelper.Administrator,
                    Name = "超级管理员"
                };
            }
            Dictionary<string, dynamic> w = new Dictionary<string, dynamic>();
            w.Add("login_name", user);
            //w.Add("psd", password);
            List<UserInfo> us = ManageHelper.DBContext.GetAllEntry<UserInfo>(w);
            if (us == null || us.Count == 0) return null;
            string s = SecurityHelper.EnCryptUnidirectional(password, key);
            if (s.Equals(us[0].PSD)) return us[0];
            return null;
        }

        /// <summary>
        /// 判断登录名称是否重复，若重复，则返回true，否则返回false
        /// </summary>
        /// <param name="loginname">登录名</param>
        /// <param name="id">修改时对应的编号</param>
        /// <returns></returns>
        public static bool IsSameLoginName(string loginname, string id = "")
        {
            Dictionary<string, dynamic> w = new Dictionary<string, dynamic>();
            w.Add("login_name", loginname);           
            List<UserInfo> us = ManageHelper.DBContext.GetAllEntry<UserInfo>(w);
            if (us == null || us.Count == 0) return false;
            if (us.Count > 1) return true;
            if (string.IsNullOrWhiteSpace(id)) return true;
            if (us[0].ID.Equals(id, StringComparison.CurrentCultureIgnoreCase)) return false;
            return true;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="loginname">登录名称</param>
        /// <param name="psd">密码</param>
        /// <returns></returns>
        public static bool UpdatePSD(string loginname, string psd)
        {
            Dictionary<string, dynamic> w = new Dictionary<string, dynamic>();
            w.Add("login_name", loginname);
            Dictionary<string, dynamic> vs = new Dictionary<string, dynamic>();
            vs.Add("psd", psd);
            vs.Add("effectivetime", ManageHelper.DBContext.GetSysDateTime());
            int rst = ManageHelper.DBContext.Update("UserInfo", vs, w);
            return rst > 0;
        }

        /// <summary>
        /// 根据用户登录名称，获取用户所有的权限信息
        /// </summary>
        /// <param name="uid">登录用户的编号</param>
        /// <returns></returns>
        public static DataTable GetUserRel(string uid)
        {
            string sql = "select rl.id, rl.roleid, rl.relid, rl.reltype, ul.user_id from ROLE_REL rl";
            sql += " left join user_role_rel ul on rl.roleid = ul.role_id ";
            sql += " where user_id = :user_id ";
            Dictionary<string, dynamic> w = new Dictionary<string, dynamic>();
            w.Add("user_id", uid);
            return ManageHelper.DBContext.GetDataTable(sql, w);
        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="uid">登录用户的编号</param>
        /// <returns></returns>
        public static DataTable GetUserRole(string uid)
        {
            string sql = "select ur.id,ur.user_id,ur.role_id,r.name as rolename,r.extent_right";
            sql += " from user_role_rel ur ";
            sql += " left join ROLE r  on ur.role_id = r.id ";
            sql += " where ur.user_id = :user_id ";
            Dictionary<string, dynamic> w = new Dictionary<string, dynamic>();
            w.Add("user_id", uid);
            return ManageHelper.DBContext.GetDataTable(sql, w);
        }

        /// <summary>
        /// 根据用户编号，获取用户所有的材料信息
        /// </summary>
        /// <param name="uid">登录用户的编号</param>
        /// <returns></returns>
        public static DataTable GetUserAttach(string uid)
        {
            string sql = "select ar.id as arid,a.id as attid,a.filename from ATTACH_REL  ar";
            sql += " left join attachment a on ar.attachid = a.id ";
            sql += " where ar.relid = :user_id ";
            sql += " order by a.uploaddate desc ";
            Dictionary<string, dynamic> w = new Dictionary<string, dynamic>();
            w.Add("user_id", uid);
            return ManageHelper.DBContext.GetDataTable(sql, w);
        }

    }
}
